package com.woniu.payservice.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class RibbitMQConfig {
    RabbitTemplate.ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {
        @Override
        public void returnedMessage(Message message, int i, String s, String s1, String s2) {
            System.out.println("返回的错误编码："+i);
            System.out.println("返回的消息："+s);
            System.out.println("交换机："+s1);
            System.out.println("路由key："+s2);
        }
    };

    RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean b, String s) {
            if(correlationData != null){
                System.out.println("接收消息的Id"+correlationData.getId());
            }
            if(!b){
                System.out.println("拒收的原因："+s);
            }
        }
    };

    @Bean//配置模板对象
    public RabbitTemplate rabbitTemplate(ConnectionFactory factory){
        RabbitTemplate template = new RabbitTemplate();
        //配置消息转换器
        template.setMessageConverter(messageConverter());
        //配置连接工厂
        template.setConnectionFactory(factory);
        //配置队列监听
        template.setReturnCallback(returnCallback);
        //配置交换机监听
        template.setConfirmCallback(confirmCallback);
        //消息强制退回
        template.setMandatory(true);
        return template;
    }

    @Bean//配置队列消息发送格式
    public Jackson2JsonMessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean//配置队列超时发送至死信交换机
    public Queue payOrderQueue(){
        //配置参数
        Map args = new HashMap<>();
        args.put("x-dead-letter-exchange","pay_order_dead_exchange");
        //配置路由key
        args.put("x-dead-letter-routing-key","pay.order.dead");
        //配置死亡时间60秒
        args.put("x-message-ttl",60000);
        return new Queue("pay_order_queue",true,false,false,args);
    }

    @Bean//配置交换机
    public TopicExchange payOrderExchange(){
        return new TopicExchange("pay_order_exchange");
    }

    @Bean//绑定交换机和队列
    public Binding bindPayOrderQueue(){
        return BindingBuilder.bind(payOrderQueue()).to(payOrderExchange()).with("pay.#");
    }

    @Bean//配置死信交换机
    public TopicExchange payOrderDeadExchange(){
        return new TopicExchange("pay_order_dead_exchange");
    }

    @Bean//配置死信队列
    public Queue payOrderDeadQueue(){
        return new Queue("pay_order_dead_queue",true,false,false);
    }

    @Bean//绑定死信队列和死信交换机并设置路由key
    public Binding bindPayOrderDeadQueue(){
        return BindingBuilder.bind(payOrderDeadQueue()).to(payOrderDeadExchange()).with("pay.order.dead");
    }

}
